package validation;

import org.netbeans.validation.api.builtin.stringvalidation.StringValidators;
import org.netbeans.validation.api.ui.swing.SwingValidationGroup;
import org.netbeans.validation.api.ui.swing.ValidationPanel;

import javax.swing.*;

public class SimpleExample {

    public static void main(String[] args) {
        //This is our actual UI
        JPanel inner = new JPanel();
        JLabel lbl = new JLabel("Enter a URL:");
        JTextField tf = new JTextField();
        tf.setColumns(40);

        // Setting the component name is important - it is used in error messages
        tf.setName("URL");

        inner.add(lbl);
        inner.add(tf);

        // Create a ValidationPanel
        // this is a panel that will show any problem with the input at the bottom with an icon
        ValidationPanel panel = new ValidationPanel();
        panel.setInnerComponent(inner);
        SwingValidationGroup group = (SwingValidationGroup) panel.getValidationGroup();

        // This is all we do to validate the URL
        group.add(tf, StringValidators.REQUIRE_NON_EMPTY_STRING,
                StringValidators.NO_WHITESPACE,
                StringValidators.URL_MUST_BE_VALID);

        // Convenience method to show a simple dialog
        if (panel.showOkCancelDialog("URL")) {
            System.out.println("User clicked OK, URL is " + tf.getText());
            System.exit(0);
        } else {
            System.out.println("User clicked cancel.");
            System.exit(1);
        }
    }
}
